Telegram Group & Telegram Channel
Embedded Academy
🔺Comparision of C++ and Posix Threads ✍️ B4b4k What is the difference between using the C++ std threads and POSIX threads? API: The API for C++ std threads and POSIX threads are different, with different function names and parameters. The C++ std thread…
One line down, more efficient: Tail Recursion

📌 B4b4k

Recursive functions are known for programmers, but it uses the call stack and has stack overflow risk. but simple change results in a big difference. this change is called "tail recursive". The tail recursion is that kind of recursion in which the recursive call is made at the end of the function.
Consider this formal recursion:

unsigned int fact(unsigned int n)
{
if (n <= 0)
return 1;
return n * fact(n - 1);
}

Can Change to the Tail-recursion version as follows:
unsigned int factTail(unsigned int n, unsigned int a)
{
if (n == 1)
return a;
return factTail(n - 1, n * a);
}
unsigned int fact(unsigned int n) { return factTail(n, 1); }

Note in this version there is no statement after the recursive call.
While computers execute recursive with the help of stacks By using tail recursive instead of formal or head recursive, compilers (such as GCC) can transform this to loop and eliminates stack overflow risk and decrease space complexity from O(n) to O(1).

#Tips #Algorithms #Cpp
@embedded



tg-me.com/embedded/2098
Create:
Last Update:

One line down, more efficient: Tail Recursion

📌 B4b4k

Recursive functions are known for programmers, but it uses the call stack and has stack overflow risk. but simple change results in a big difference. this change is called "tail recursive". The tail recursion is that kind of recursion in which the recursive call is made at the end of the function.
Consider this formal recursion:

unsigned int fact(unsigned int n)
{
if (n <= 0)
return 1;
return n * fact(n - 1);
}

Can Change to the Tail-recursion version as follows:
unsigned int factTail(unsigned int n, unsigned int a)
{
if (n == 1)
return a;
return factTail(n - 1, n * a);
}
unsigned int fact(unsigned int n) { return factTail(n, 1); }

Note in this version there is no statement after the recursive call.
While computers execute recursive with the help of stacks By using tail recursive instead of formal or head recursive, compilers (such as GCC) can transform this to loop and eliminates stack overflow risk and decrease space complexity from O(n) to O(1).

#Tips #Algorithms #Cpp
@embedded

BY Embedded Academy


Warning: Undefined variable $i in /var/www/tg-me/post.php on line 283

Share with your friend now:
tg-me.com/embedded/2098

View MORE
Open in Telegram


Embedded Academy Telegram | DID YOU KNOW?

Date: |

What is Telegram Possible Future Strategies?

Cryptoassets enthusiasts use this application for their trade activities, and they may make donations for this cause.If somehow Telegram do run out of money to sustain themselves they will probably introduce some features that will not hinder the rudimentary principle of Telegram but provide users with enhanced and enriched experience. This could be similar to features where characters can be customized in a game which directly do not affect the in-game strategies but add to the experience.

Telegram hopes to raise $1bn with a convertible bond private placement

The super secure UAE-based Telegram messenger service, developed by Russian-born software icon Pavel Durov, is looking to raise $1bn through a bond placement to a limited number of investors from Russia, Europe, Asia and the Middle East, the Kommersant daily reported citing unnamed sources on February 18, 2021.The issue reportedly comprises exchange bonds that could be converted into equity in the messaging service that is currently 100% owned by Durov and his brother Nikolai.Kommersant reports that the price of the conversion would be at a 10% discount to a potential IPO should it happen within five years.The minimum bond placement is said to be set at $50mn, but could be lowered to $10mn. Five-year bonds could carry an annual coupon of 7-8%.

Embedded Academy from pl


Telegram Embedded Academy
FROM USA